package com.vf.editor.mvc.scene.point;

import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.vf.editor.common.controller.BaseController;
import com.vf.editor.common.model.resources.Icon;
import com.vf.editor.common.model.resources.SceneLayer;
import com.vf.editor.common.model.resources.ScenePoint;
import com.vf.editor.common.util.UuidUtil;

@Path(value = "/scene/point", viewPath = "/views/scene/point/")
public class PointController extends BaseController{
	
	public void library() {
		this.render("library.html");
	}
	
	public void edit() {
		this.render("edit.html");
	}
	
	public void listDataForIcon() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String name = getPara("name");
		
		String sqlExceptSelect = " from " + Icon.TABLE_NAME + " o  where 1=1 ";
		
		if (!StrKit.isBlank(name)) {
			sqlExceptSelect += " and o.name like '%" + name + "%'  ";
		}
		sqlExceptSelect += "  order by   o.createTime desc ";
		Page<Icon> page = Icon.dao.paginate(pageNumber, pageSize, "select o.* ", sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}
	
	public void save() {
		ScenePoint model = getModel(ScenePoint.class);
		if (ScenePoint.dao.findById(model.getId())!=null) {
			if (model.update()) {
				SceneLayer sceneLayer=SceneLayer.dao.findById(model.getId());
				if(sceneLayer!=null) {
					sceneLayer.setId(model.getId());
					if(!StrKit.isBlank(model.getName())) sceneLayer.setName(model.getName());
					if(!StrKit.isBlank(model.getCode())) sceneLayer.setCode(model.getCode());
					if(!StrKit.isBlank(model.getType())) sceneLayer.setType(model.getType());
					sceneLayer.update();
				}
				renderSuccess("更新成功！",model);
			} else {
				renderError("更新失败！");
			}
		} else {
			if(StrKit.isBlank(model.getId())) model.setId(UuidUtil.getUUID());
			if (model.save()) {
				SceneLayer sceneLayer=new SceneLayer();
				sceneLayer.setId(model.getId());
				sceneLayer.setName(model.getName());
				sceneLayer.setCode(model.getCode());
				sceneLayer.setType(model.getType());
				sceneLayer.setSceneId(model.getSceneId());
				sceneLayer.setParentId(model.getSceneId());
				if(sceneLayer.save()) {
					renderSuccess("保存成功！",model);
				}else {
					renderError("保存失败！");
					try {
						model.delete();
					}catch (Exception e) {
					}
				}
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	public void delete() {
		String id=this.getPara("id");
		SceneLayer sceneLayer=SceneLayer.dao.findFirst("select * from "+SceneLayer.TABLE_NAME+" where id=? ",id);
		if(sceneLayer!=null) {
			if(sceneLayer.delete()) {
				ScenePoint.dao.deleteById(sceneLayer.getId());
				this.renderSuccess("删除成功！", sceneLayer);
			}else {
				this.renderError("删除失败！");
			}
		}else {
			this.renderError("数据不存在！");
		}
	}
	

}
